package edu.fjut.se1603.yuan.util;

import java.util.List;
import java.util.Scanner;

public class KeyInput {
    private static Scanner sc = new Scanner(System.in);

    /**
     * 获取数字
     * @return 返回数字
     */
    public static int getInt() {
        return _getInt();
    }

    /**
     * 获取数字
     * @return 返回字符串
     */
    public static String getString(){
        return sc.nextLine();
    }

    /**
     * 验证非数字
     * @return 返回非数字
     */
    private static int _getInt()
    {
        int result = 0;
        boolean isSucceed = false;
        try
        {
            result = Integer.parseInt(sc.nextLine());
            isSucceed = true;
        }catch(NumberFormatException e){}

        while(!isSucceed)
        {
            System.out.print("非数字,重新输入:");
            try
            {
                result = Integer.parseInt(sc.nextLine());
                isSucceed = true;
            }catch(NumberFormatException e){}
        }
        return result;
    }

    /**
     * 获取同意还是否决
     * @return 成功/否决
     */
    public static boolean getYON()
    {

        String result = getString();
        while(!result.equalsIgnoreCase("y") && !result.equalsIgnoreCase("n")) {
            System.out.print("重新输入(y/n)：");
            result = getString();
        }
        return result.equalsIgnoreCase("y");
    }

    /**
     * 获取一个int且值为collection之一
     * @param collection 可能的值
     * @return int
     */
    public static int getInt(List<Integer> collection)
    {
        int result;
        result = getInt();
        while(!collection.contains(result))
        {
            System.out.print("输入有误,重新输入:");
            result = _getInt();
        }
        return result;
    }
}
